package org.flexthinker.module
{
    import flash.events.Event;

    import mx.events.FlexEvent;

    import mx.events.ModuleEvent;

    import mx.logging.Log;
    import mx.logging.targets.TraceTarget;
    import mx.modules.Module;

    import org.flexthinker.module.impl.ModuleDescriptor;
    import org.flexunit.async.Async;
    import org.fluint.uiImpersonation.UIImpersonator;
    import org.hamcrest.assertThat;
    import org.hamcrest.object.equalTo;

    import spark.components.Button;

    public class ModuleViewLoaderTest
    {
        //to be used when tested from IJ IDE
//        private static const MODULE_URL:String = "test-classes/test/ButtonModule.swf";
//        private static const MODULE_SKIN_URL:String = "test-classes/test/Skin1_buttonModule.swf";

        // to the used when tested with maven
        private static const MODULE_URL:String = "test/ButtonModule.swf";
        private static const MODULE_SKIN_URL:String = "test/Skin1_buttonModule.swf";

        private var loader:ModuleViewLoader;

        private var btn:Button;

        public function ModuleViewLoaderTest()
        {
        }

        [Before(async,ui)]
        public function setUp():void
        {
            loader = new ModuleViewLoader();

            btn = new Button();
            btn.label = "Test";
            btn.x = 300;

            Async.proceedOnEvent(this, loader, FlexEvent.CREATION_COMPLETE, 200);

            UIImpersonator.addChild(loader);
            UIImpersonator.addChild(btn);

            Log.addTarget(new TraceTarget());

        }

        [After(async,ui)]
        public function tearDown():void
        {
            loader.unload();
            UIImpersonator.removeChild(loader);
            loader = null;
        }


        [Test(async,ui,timeout=2000)]
        public function testInitialization():void
        {
            loader.url = MODULE_URL;
            assertThat("moduleViewLoader should not have any dependency",
                    ModuleDescriptor(loader.moduleDescriptor).dependencies.length, equalTo(0));

            loader.stylesheetUrl = MODULE_SKIN_URL;
            assertThat("moduleViewLoader should  have 1 dependency",
                    ModuleDescriptor(loader.moduleDescriptor).dependencies.length, equalTo(1));
        }

        [Test(async,ui,timeout="6000")]
        public function testLoadingModuleWithSkin():void
        {
            loader.url = MODULE_URL;
            loader.stylesheetUrl = MODULE_SKIN_URL;

            Async.handleEvent(this, loader, ModuleEvent.READY, moduleWithSkin_readyHandler, 4000);

            loader.load();
        }

        private function moduleWithSkin_readyHandler(event:ModuleEvent, param:Object):void
        {
            assertThat("not enough bytes loaded", loader.bytesLoaded > 1000000, equalTo(true));

            Async.handleEvent(this, loader, Event.COMPLETE, checkStyles, 1000);
        }

        private function checkStyles(event:Event, param:Object):void
        {
            //test that skin style has been applied to the module, but not to the main app
            var module:Module = Module(loader.getElementAt(0));
            // this next assert works when executed from IDE, but not when executed from mvn.
            // needs a bit more investigation. it could be from the sequence within test suite
//           assertThat("module's style should have NOT be applied to the main app", btn.getStyle("color"), equalTo(0x000000));
            assertThat("module's style was not loaded correctly",
                    module.styleManager.getStyleDeclaration("spark.components.Button").getStyle("color"),
                    equalTo(0xFF0000));

            loader.unload();
//            Async.handleEvent(this, loader, Event.COMPLETE, checkStyles, 5000 );
        }

    }
}